package br.com.linkin.mi.utils;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringWriter;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class IO {

    public static byte[] toByteArray(InputStream in) throws Exception {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int next;
        while ((next = in.read()) > -1) {
            out.write(next);
        }
        out.flush();
        return out.toByteArray();
    }

    public static String readToString(InputStream in) throws Exception {
        return readToString(in, Charset.defaultCharset().name());
    }

    public static String readToString(InputStream in, String charset)
            throws Exception {
        StringWriter out = new StringWriter();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in,
                charset));
        int ch;
        while ((ch = reader.read()) != -1) {
            out.write(ch);
        }
        return out.toString();
    }

    public static InputStream load(String resource) {
        return Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
    }

    public static URL loadURL(String resource) {
        return Thread.currentThread().getContextClassLoader().getResource(resource);
    }

    public static void zipFiles(OutputStream fileOut, String[] fileNames,
            ByteArrayInputStream[] files) {
        ZipOutputStream out = new ZipOutputStream(fileOut);
        byte[] buf = new byte[1024];
        try {
            for (int i = 0; i < fileNames.length; i++) {
                String fileName = fileNames[i];
                ByteArrayInputStream in = files[i];
                out.putNextEntry(new ZipEntry(fileName));
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.closeEntry();
                in.close();

            }
            out.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<ByteArrayOutputStream> unZipFiles(InputStream fileZipped)
            throws IOException {
        try {
            final int buffer = 2048;
            List<ByteArrayOutputStream> byteArrayOutputStreams = new ArrayList<ByteArrayOutputStream>();
            ByteArrayOutputStream dest = null;
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(
                    fileZipped));
            while ((zis.getNextEntry()) != null) {
                int count;
                byte data[] = new byte[buffer];
                dest = new ByteArrayOutputStream();
                while ((count = zis.read(data, 0, buffer)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
                dest.close();
                byteArrayOutputStreams.add(dest);
            }
            zis.close();
            return byteArrayOutputStreams;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
